<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class getPaginationLink
{
    private $numPage;
    private $numPageView;
    
    private $idPagi;
    private $classActive;
    private $classInactive;

    function __construct($totalQuery, $perPage, $numPageView)
    {
        $perPage = !is_numeric($perPage) ? 10 : $perPage;
        $numPageView = !is_numeric($numPageView) ? 7 : $numPageView;
        $this->numPage = ceil($totalQuery/$perPage);
        $this->numPageView = $numPageView;
    }
    
    public function setStylePagination($idPagi, $classActive, $classInactive)
    {
        $this->idPagi = $idPagi;
        $this->classActive = $classActive;
        $this->classInactive = $classInactive;
    }
    
    private function getLoop($currentPage)
    {
        $data = array();
        $numOutCurrent = ceil($this->numPageView/2);
        if($currentPage >= $this->numPageView) {
            $data['startLoop'] = $currentPage - $numOutCurrent;
            if($this->numPage > $currentPage + $numOutCurrent)
                $data['endLoop'] = $currentPage + $numOutCurrent;
            elseif ($currentPage <= $this->numPage && $currentPage > $this->numPage - ($this->numPageView - 1)) {
                $data['startLoop'] = $this->numPage - ($this->numPageView - 1);
                $data['endLoop'] = $this->numPage;
            } else {
                $data['endLoop'] = $this->numPage;
            }
        } else {
            $data['startLoop'] = 1;
            if($this->numPage > $this->numPageView)
                $data['endLoop'] = $this->numPageView;
            else
                $data['endLoop'] = $this->numPage;
        }
        return $data;
    }
    
    public function renderLink($currentPage, $showButton = true, $showGoTo = false)
    {
        $dataLoop = $this->getLoop($currentPage);
        $msg = "<ul id='$this->idPagi'>";
        
        if($showButton && $currentPage > 1) {
            $previousLink = $currentPage - 1;
            $msg .= "<li id='link_$previousLink' class='$this->classActive'><<</li>";
        } elseif ($showButton) {
            $msg .= "<li class='$this->classInactive'><<</li>";
        }
        
        for($i = $dataLoop['startLoop']; $i<= $dataLoop['endLoop']; $i++) {
            if($currentPage == $i)
                $msg .= "<li id='link_$i' style='color:#fff; background-color:#006699' class='$this->classActive linkNaviSelect'>{$i}</li>";
            else
                $msg .= "<li id='link_$i' class='$this->classActive'>{$i}</li>";
        }
        
        if ($showButton && $currentPage < $this->numPage) {
            $nextLink = $currentPage + 1;
            $msg .= "<li id='link_$nextLink' class='$this->classActive'>>></li>";
        } elseif($showButton) {
            $msg .= "<li class='$this->classInactive'>>></li>";
        }
        
        if($showGoTo && $this->numPage != 0 && $this->numPage > $this->numPageView)
            $msg .= "<input type='text' id='gotoLink' class='gotoLink' size='1' style='margin:0px 2px;float:left'/>
                     <input type='button' id='btnGoToLink' class='btnGoToLink' style='float:left' value='Đến'/>";
        
        $msg .= "</ul>";
        return $msg;
    }
    
    public function getNumPage()
    {
        return $this->numPage;
    }

}
?>
